<?php

/**
 * Created by IntelliJ IDEA.
 * User: jirapatjantsakool
 * Date: 3/7/14
 * Time: 12:24 AM
 */
require('../model/content.php');

require('../db/database.php');
require('../service/common_service.php');

class content_service extends common_service
{

    public function findAll()
    {
        global $db;
        $sql = "select * FROM cf_content order by catid ,created";
        $query = $db->query($sql);

        $content_list = array();

        while ($result = $db->fetch_array($query)) {

            $content = new content();

            $content->setId($result['id']);
            $content->setTitle($result['title']);
            $content->setAlias($result['alias']);
            $content->setTitleAlias($result['title_alias']);
            $content->setIntrotext($result['introtext']);
            $content->setFulltext($result['fulltext']);
            $content->setCatid($result['catid']);
            $content->setUserid($result['userid']);
            $content->setCreated(apputil:: getDateStringFormat($result['created'], 'd/m/Y'));
            $content->setModified(apputil:: getDateStringFormat($result['modified'], 'd/m/Y'));
            $content->setPublishdate(apputil:: getDateStringFormat($result['publishdate'], 'd/m/Y'));
            $content->setStatus($result['status']);

            $content_list[] = $content;
        }
        return $content_list;
    }


    public function findContentByCategoryId($categoryId)
    {
        global $db;
        $content_list = array();

        $sql = "SELECT id, "
            . " title, alias, title_alias, introtext, "
            . " `fulltext`, userid, created, "
            . " modified, publishdate, status "
            . " FROM cf_content c "
            . " WHERE catid = ? ";

        if ($stmt = $db->prepare($sql)) {

            //binding param
            mysqli_stmt_bind_param($stmt, 'i', $categoryId);

            $db->execute_statement($stmt);

            $rowMaps = array();
            mysqli_stmt_bind_result($stmt, $rowMaps['id'],
                                           $rowMaps['title'],
                                           $rowMaps['alias'],
                                           $rowMaps['title_alias'],
                                           $rowMaps['introtext'],
                                           $rowMaps['fulltext'],
                                           $rowMaps['userid'],
                                           $rowMaps['created'],
                                           $rowMaps['modified'],
                                           $rowMaps['publishdate'],
                                           $rowMaps['status']);

            //TODO check number record
            while ($db->fetch_statement($stmt)) {
                $content = new content();
                $content->setId($rowMaps['id']);
                $content->setTitle($rowMaps['title']);
                $content->setAlias($rowMaps['alias']);
                $content->setTitleAlias($rowMaps['title_alias']);
                $content->setIntrotext($rowMaps['introtext']);
                $content->setFulltext($rowMaps['fulltext']);
                $content->setCatid($categoryId);
                $content->setUserid($rowMaps['userid']);
                $content->setCreated(apputil:: getDateStringFormat($rowMaps['created'], 'd/m/Y'));
                $content->setModified(apputil:: getDateStringFormat($rowMaps['modified'], 'd/m/Y'));
                $content->setPublishdate(apputil:: getDateStringFormat($rowMaps['publishdate'], 'd/m/Y'));
                $content->setStatus($rowMaps['status']);

                $content_list[] = $content;
            }
            $db->close_connection();
        }
        return $content_list;
    }

    public function findContentById($id)
    {
        global $db;
        $content = new content();

        $sql = "SELECT "
            . " title, alias, title_alias, introtext, "
            . " `fulltext`, catid, userid, created, "
            . " modified, publishdate, status "
            . " FROM cf_content c "
            . " WHERE id = ?";

        if ($stmt = $db->prepare($sql)) {

            //binding param
            mysqli_stmt_bind_param($stmt, 'i', $id);

            $db->execute_statement($stmt);

            $rowMaps = array();
            mysqli_stmt_bind_result($stmt, $rowMaps['title'],
                                           $rowMaps['alias'],
                                           $rowMaps['title_alias'],
                                           $rowMaps['introtext'],
                                           $rowMaps['fulltext'],
                                           $rowMaps['catid'],
                                           $rowMaps['userid'],
                                           $rowMaps['created'],
                                           $rowMaps['modified'],
                                           $rowMaps['publishdate'],
                                           $rowMaps['status']);

            //TODO check number record
            if ($db->fetch_statement($stmt)) {

                $content->setId($id);
                $content->setTitle($rowMaps['title']);
                $content->setAlias($rowMaps['alias']);
                $content->setTitleAlias($rowMaps['title_alias']);
                $content->setIntrotext($rowMaps['introtext']);
                $content->setFulltext($rowMaps['fulltext']);
                $content->setCatid($rowMaps['catid']);
                $content->setUserid($rowMaps['userid']);
                $content->setCreated(apputil:: getDateStringFormat($rowMaps['created'], 'd/m/Y'));
                $content->setModified(apputil:: getDateStringFormat($rowMaps['modified'], 'd/m/Y'));
                $content->setPublishdate(apputil:: getDateStringFormat($rowMaps['publishdate'], 'd/m/Y'));
                $content->setStatus($rowMaps['status']);

            }
            $db->close_connection();
        }
        return $content;
    }

    public function delete($content)
    {
        global $db;
        static $flag = false;

        $sql = "DELETE FROM cf_content "
            . " WHERE id = ? ";

        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'i', $content->getId());

            $flag = $db->execute_statement($stmt);
        }
        $db->close_connection();
        return $flag;
    }

    public function update($content)
    {
        global $db;
        static $flag = false;

        $sql = "UPDATE cf_content c SET "
            . " title = ?, alias = ?, title_alias = ?, introtext = ?, "
            . " `fulltext` = ?, catid = ?, userid = ?,  "
            . " modified = CURDATE(), publishdate =  STR_TO_DATE(?, '%d/%m/%Y/') , status = ? "
            . " WHERE id = ? ";

        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'sssssiissi', $content->getTitle(),
                                                        $content->getAlias(),
                                                        $content->getTitleAlias(),
                                                        $content->getIntrotext(),
                                                        urldecode($content->getFulltext()),
                                                        $content->getCatid(),
                                                        $content->getUserid(),
//                                                           $content->getCreated(),
//                                                           now(),
                                                        $content->getPublishdate(),
                                                        $content->getStatus(),
                                                        $content->getId());
            $flag = $db->execute_statement($stmt);
        }
        $db->close_connection();
        return $flag;
    }

    public function create($content)
    {
        global $db;
        static $flag = false;

        $sql = "INSERT INTO cf_content "
            . "(title, alias, title_alias, introtext, "
            . " `fulltext`, catid, userid, created, "
            . " modified, publishdate, status)"
            . " VALUES(?, ?, ?, ? ,? ,? ,? ,CURDATE() ,CURDATE() , STR_TO_DATE(?, '%d/%m/%Y/') ,?) ";


        if ($stmt = $db->prepare($sql)) {
            //binding param
            mysqli_stmt_bind_param($stmt, 'sssssiiss', $content->getTitle(),
                                                       $content->getAlias(),
                                                       $content->getTitleAlias(),
                                                       $content->getIntrotext(),
                                                       urldecode($content->getFulltext()),
                                                       $content->getCatid(),
                                                       $content->getUserid(),
//                                                         now(),
//                                                         now(),
                                                       $content->getPublishdate(),
                                                       $content->getStatus());
            $flag = $db->execute_statement($stmt);
        }

        $db->close_connection();
        return $flag;
    }

    public function findContentByCriteria($content)
    {
        global $db;
        $content_list = array();

        $sql = "SELECT id, "
            . " title, alias, title_alias, introtext, "
            . " `fulltext`, catid, userid, created, "
            . " modified, publishdate, status "
            . " FROM cf_content "
            . " WHERE title LIKE concat ('%',?,'%') "
            . " OR alias LIKE concat ('%',?,'%') "
            . " OR title_alias LIKE concat ('%',?,'%') ";

        if ($stmt = $db->prepare($sql)) {

            //binding param
            mysqli_stmt_bind_param($stmt, 'sss', $content->getTitle(),
                                                 $content->getAlias(),
                                                 $content->getTitleAlias());

            $db->execute_statement($stmt);

            $rowMaps = array();
            mysqli_stmt_bind_result($stmt, $rowMaps['id'],
                                           $rowMaps['title'],
                                           $rowMaps['alias'],
                                           $rowMaps['title_alias'],
                                           $rowMaps['introtext'],
                                           $rowMaps['fulltext'],
                                           $rowMaps['catid'],
                                           $rowMaps['userid'],
                                           $rowMaps['created'],
                                           $rowMaps['modified'],
                                           $rowMaps['publishdate'],
                                           $rowMaps['status']);

            //TODO check number record
            while ($db->fetch_statement($stmt)) {
                $content = new content();

                $content->setId($rowMaps['id']);
                $content->setTitle($rowMaps['title']);
                $content->setAlias($rowMaps['alias']);
                $content->setTitleAlias($rowMaps['title_alias']);
                $content->setIntrotext($rowMaps['introtext']);
                $content->setFulltext($rowMaps['fulltext']);
                $content->setCatid($rowMaps['catid']);
                $content->setUserid($rowMaps['userid']);
                $content->setCreated(apputil:: getDateStringFormat($rowMaps['created'], 'd/m/Y'));
                $content->setModified(apputil:: getDateStringFormat($rowMaps['modified'], 'd/m/Y'));
                $content->setPublishdate(apputil:: getDateStringFormat($rowMaps['publishdate'], 'd/m/Y'));
                $content->setStatus($rowMaps['status']);

                $content_list[] = $content;
            }
            $db->close_connection();
        }
        return $content_list;
    }

    public function isValidateTokenKey($token)
    {
        global $db;
        $common_service = new common_service();
        return $common_service->validateTokenKey($db, $token, 'token');
    }

}

$content_service = new content_service();
//var_dump($content_service->isValidateTokenKey('abc1234'));

?>
